package com.ethan.rabbitdelayproducer;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    public static final String EXCHANGE_NAME="ethan_delay_exchange";
    public static final String EXCHANGE_NAME_DEAD="ethan_delay_exchange_01";

    @Bean("exchange")
    public Exchange exchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).build();
    }

    @Bean("deadExchange")
    public Exchange deadExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME_DEAD).build();
    }

    @Bean("delayQueue")
    public Queue queue(){
        return QueueBuilder.durable("delay_queue_01").build();
    }

    @Bean("queue")
    public Queue deadQueue(){
        return QueueBuilder.durable("delay_queue").ttl(10*1000).deadLetterExchange(EXCHANGE_NAME_DEAD).deadLetterRoutingKey("delay_01.msg").build();
    }
    @Bean
    public Binding bing(@Qualifier("exchange") Exchange exchange,
                        @Qualifier("queue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("delay.#").noargs();
    }

    @Bean
    public Binding bing01(@Qualifier("deadExchange") Exchange exchange,
                          @Qualifier("delayQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("delay_01.#").noargs();
    }
}
